/**
 * @author hdeom
 * @date 2014. 1. 28.
 *
 */

package h.d.bluetooth.visitor;

import h.d.bluetooth.visitor.common.VaE;
import h.d.bluetooth.visitor.sort.SortData;
import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @author hdeom
 * @date 2014. 1. 28.
 *
 */
public class MainActivity extends Activity implements OnClickListener{

	protected String TAG = "MainActivity";
	private Button btnBluetoothName;
	private Button btnScan;
	private Button btnHistory;
	private Button btnSetting;

	private TextView tvBluetoothName;
	private TextView tvMainTxt;

	private WebView wvAd;
	private BluetoothAdapter mBluetoothAdapter;
	private EditText editText;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);


		btnBluetoothName = (Button) findViewById(R.id.bluetooth_name_fix_btn);
		btnScan = (Button) findViewById(R.id.scan);
		btnHistory = (Button) findViewById(R.id.history);
		btnSetting = (Button) findViewById(R.id.setting);

		btnBluetoothName.setOnClickListener(this);
		btnScan.setOnClickListener(this);
		btnHistory.setOnClickListener(this);
		btnSetting.setOnClickListener(this);

		tvBluetoothName = (TextView) findViewById(R.id.bluetooth_name_fix_txt);
		tvMainTxt = (TextView) findViewById(R.id.main_txt);


		wvAd = (WebView) findViewById(R.id.ad);
		wvAd.setVisibility(View.GONE);


		mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

		tvBluetoothName.setText(getBluetoothNameTxt());


	}

	/**
	 * @author hdeom
	 * @date 2014. 1. 28.
	 * @param
	 * @return void
	 *
	 **/

	private String getBluetoothNameTxt() {

		String r = "";

		r = "블루투스 이름 : "+mBluetoothAdapter.getName();
		r = r + "\n블루투스 주소 : "+mBluetoothAdapter.getAddress();
		r = r + "\n블루투스 상태 : "+(mBluetoothAdapter.isEnabled() ? "켜짐" : "꺼짐");

		return r;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public void onClick(View v) {

		Intent i = new Intent();
		switch(v.getId()){
		case R.id.bluetooth_name_fix_btn:
			makeInputPopup(getString(R.string.device_info_fix));
			break;

		case R.id.scan:
			i.setClass(MainActivity.this, BluetoothVisitor.class);
			startActivity(i);
			break;

		case R.id.history:
			break;

		case R.id.setting:
			i.setClass(MainActivity.this, Setup.class);
			startActivity(i);
			break;

		default:
			break;
		}
	}


	public void makeInputPopup(String title) {
		AlertDialog Alertdevcofig;
		AlertDialog.Builder devConfig = new AlertDialog.Builder(this)
		.setTitle(title)
		.setView(CreateditText())
		.setCancelable(false)
		.setPositiveButton(getString(R.string.button_ok), new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				if(!TextUtils.isEmpty(editText.getText().toString())){
					mBluetoothAdapter.setName(editText.getText().toString());
					tvBluetoothName.setText(getBluetoothNameTxt());
				}
			}
		});
		devConfig.setNegativeButton(getString(R.string.button_cancel), new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		Alertdevcofig = devConfig.create();
		Alertdevcofig.show();
	}

	/**
	 * CreateditText :
	 * @return View : 입력 에디터 뷰 리턴
	 */
	private View CreateditText(){
		LinearLayout edtlinear = new LinearLayout(this);
		editText = new EditText(this);
		editText.setHeight(10);
		editText.setWidth(100);
		edtlinear.setOrientation(LinearLayout.VERTICAL);
		edtlinear.addView(editText);
		return edtlinear;
	}


}
